package com.winsdom.utils;


import com.winsdom.exception.BaseException;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//获取http请求头中的信息
public class ServletUtil {
    public static String getRequestParam(String value, String def) {
        String parameter = getRequest().getParameter(value);
        if (parameter == null) {
            return def;
        }
        return parameter;
    }

    public static String getRequestParam(String value) {
        return getRequest().getParameter(value);
    }


    public static Long getHeadUserId() {
        String userId = getRequestHeader("UserId");
        if (userId == null) {
            throw new BaseException("没有找到userID");
        }
        return Long.parseLong(userId);
    }


    public static String getRequestHeader(String value) {
        return getRequest().getHeader(value);
    }

    public static HttpServletRequest getRequest() {

        return getServlet().getRequest();
    }

    public static ServletRequestAttributes getServlet() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

}
